//
//  MTPartyDetailHeaderCell.swift
//  MT
//
//  Created by ooops on 2020/5/26.
//  Copyright © 2020 张强. All rights reserved.
//

import UIKit
import Foundation
import Eureka
import MTCategory

public final class MTPartyHeaderRow: MTPartyDetailHeaderRow, RowType {
    required public init(tag: String?) {
        super.init(tag: tag)
    }
}

open class MTPartyDetailHeaderRow: MTPartyDetailHeaderCellRow {
    required public init(tag: String?) {
        super.init(tag: tag)
    }
}

open class MTPartyDetailHeaderCell: Cell<String>, CellType {
    
    var model: MTPartyListModel? {
        didSet {
            partyImageView.mtSetImage(model?.detailResult?.href, nil)
            titleLabel.text = model?.detailResult?.name
            if model?.detailResult?.certification == true {
                partyTypeLabel.text = "商"
                partyTypeLabel.isHidden = false
            } else {
                partyTypeLabel.isHidden = true
            }
            partyTypeLabel.text = model?.detailResult?.certification == true ? "商" : ""
            if let invite = model?.detailResult?.inviteNum {
                partyInviteLabel.text = "限\(invite)人"
            }
            avatarImageView.mtSetImage(model?.detailResult?.userHref, nil)
            if let flag = model?.detailResult?.anonymityFlag {
                publishNickNameLabel.text = flag == 1 ? "匿名发布" : model?.detailResult?.nickname
                coverImageView.isHidden = (flag != 1)
            }
            
            
            /// 招募中
//            case wating = 1
//            /// 已经开始 - 进行中
//            case started = 2
//            /// 已经结束
//            case ended = 3
//            /// 申请中 - 未通过审核
//            case invite
//            /// 被拒绝
//            case reject
//            /// 已通过申请
//            case accept
            switch model?.detailResult?.status {
            case .wating:
                statusLabel.text = "招募中"
            case .started:
            statusLabel.text = "进行中"
            case .ended:
            statusLabel.text = "已结束"
            case .invite:
            statusLabel.text = "已申请"
            case .reject:
            statusLabel.text = ""
            default:
                statusLabel.text = "已报名"
            }
        }
    }

    public required init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
    }

    required public init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    lazy var partyImageView: UIImageView = {
        let imageV = UIImageView()
        imageV.contentMode = .scaleAspectFill
//        imageV.layer.borderColor = UIColor.red.cgColor
//        imageV.layer.borderWidth = 1
        imageV.layer.masksToBounds = true
        return imageV
    }()
    
    lazy var coverImageView: UIImageView = {
        let imageV = UIImageView()
        imageV.image = UIImage(named: "task_detail_cover")
        imageV.layer.masksToBounds = true
        imageV.contentMode = .scaleAspectFill
        return imageV
    }()
    
    /// 显示模式
    lazy var titleLabel: UILabel = {
        let l = UILabel()
        l.font = PingFangSCSemiboldFont(16)
        l.textColor = UIColor.text.x1d1a1a
        l.text = "名称"
        l.setContentHuggingPriority(.defaultHigh, for: .horizontal)
        return l
    }()
    
    lazy var partyTypeLabel: UILabel = {
        let l = UILabel()
        l.font = PingFangSCSemiboldFont(12)
        l.textColor = UIColor.white
        l.text = "下"
        l.backgroundColor = UIColor.blue
        l.textAlignment = .center
        return l
    }()
    
    lazy var partyInviteLabel: UILabel = {
        let l = UILabel()
        l.font = PingFangSCMediumFont(12)
        l.textColor = UIColor.text.x1d1a1a
        l.text = "限7人"
        return l
    }()
    
    lazy var statusLabel: UILabel = {
        let l = UILabel()
        l.font = PingFangSCMediumFont(12)
        l.textColor = UIColor.text.x9b9b9b
        l.lineBreakMode = .byCharWrapping
        l.textAlignment = .right
        return l
    }()
    
    lazy var lineView: UIView = {
        let v = UIView()
        v.backgroundColor = UIColor.seprateLine.x707070
        return v
    }()
    
    lazy var avatarImageView: UIImageView = {
        let imageV = UIImageView()
        imageV.contentMode = .scaleAspectFill
        imageV.layer.cornerRadius = 30
        imageV.layer.masksToBounds = true
        return imageV
    }()
    
    lazy var publishNickNameLabel: UILabel = {
        let l = UILabel()
        l.font = PingFangSCMediumFont(16)
        l.textColor = UIColor.text.x1d1a1a
        return l
    }()
    
    lazy var topLineView: UIView = {
        let v = UIView()
        v.backgroundColor = UIColor.seprateLine.x707070
        return v
    }()
    
    open override func setup() {
        super.setup()
        backgroundColor = UIColor.theme.background
        selectionStyle = .none
        textLabel?.isHidden = true
        detailTextLabel?.isHidden = true
        editingAccessoryType =  .none
        row.disabled = Condition(booleanLiteral: true)
        
        contentView.addSubview(partyImageView)
        contentView.addSubview(avatarImageView)
        contentView.addSubview(publishNickNameLabel)
        contentView.addSubview(topLineView)
        
        
        contentView.addSubview(titleLabel)
        contentView.addSubview(partyTypeLabel)
        contentView.addSubview(partyInviteLabel)
        contentView.addSubview(statusLabel)
        contentView.addSubview(lineView)
        contentView.addSubview(coverImageView)
        
        makeConstraint()
    }
    
    
    
    private func makeConstraint() {
        avatarImageView.snp.makeConstraints { (make) in
            make.centerY.equalTo(partyImageView.snp.bottom)
            make.left.equalToSuperview().offset(18)
            make.size.equalTo(CGSize(width: 60, height: 60))
        }
        
        coverImageView.snp.makeConstraints { (make) in
            make.center.equalTo(avatarImageView)
            make.size.equalTo(avatarImageView)
        }
        
        topLineView.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(18)
            make.right.equalToSuperview().offset(-18)
            make.height.equalTo(0.5)
            make.top.equalTo(avatarImageView.snp.bottom).offset(18)
        }
        
        publishNickNameLabel.snp.makeConstraints { (make) in
            //make.centerY.equalTo(avatarImageView.snp.bottom).offset(-5)
            make.left.equalTo(avatarImageView.snp.right).offset(5)
            make.right.equalToSuperview().offset(-12)
            make.top.equalTo(partyImageView.snp.bottom).offset(5)
        }
        
        partyImageView.snp.makeConstraints { (make) in
            make.left.right.top.equalToSuperview()
            make.height.equalTo(220.auto())
        }
        
        lineView.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(18)
            make.right.equalToSuperview().offset(-18)
            make.height.equalTo(0.5)
            make.bottom.equalToSuperview()
        }
        
        titleLabel.snp.makeConstraints { (make) in
            make.top.equalTo(topLineView.snp.bottom).offset(8)
            make.left.equalToSuperview().offset(18)
            //make.width.greaterThanOrEqualTo(30)
        }
        
        partyTypeLabel.snp.makeConstraints { (make) in
            make.left.equalTo(titleLabel.snp.right).offset(5)
            make.centerY.equalTo(titleLabel)
            make.size.equalTo(CGSize(width: 14, height: 14))
        }

        partyInviteLabel.snp.makeConstraints { (make) in
            make.left.equalTo(titleLabel)
            make.top.equalTo(titleLabel.snp.bottom)
            make.bottom.equalToSuperview().offset(-9)
        }
        
        statusLabel.snp.makeConstraints { (make) in
            make.centerY.equalTo(titleLabel.snp.bottom)
            make.right.equalToSuperview().offset(-18)
        }
    }

    deinit {
//        datePicker.removeTarget(self, action: nil, for: .allEvents)
    }

    open override func update() {
        super.update()
//        if row.value != nil {
//            contentLabel.text = row.displayValueFor?(row.value)
//            contentLabel.font = PingFangSCSemiboldFont(14)
//            contentLabel.textColor = UIColor.text.x9b9b9b
//        }
    }

    open override func didSelect() {
        super.didSelect()
        row.deselect()
    }

    override open var inputView: UIView? {
        return nil
    }
}

open class MTPartyDetailHeaderCellRow: Row<MTPartyDetailHeaderCell>, NoValueDisplayTextConformance {

    open var noValueDisplayText: String? = nil

    required public init(tag: String?) {
        super.init(tag: tag)
        displayValueFor = { [unowned self] value in
            guard let val = value else { return nil }
            return val
        }
    }
}



